package leetcode;

import java.util.Arrays;

public class LC20 {
    /*
    00  03
    03  33
    33  30
    30  00

    01  13
    13  32
    32  20
    20  01

     */
    public static void main(String[] args) {
        LC20 lc20 = new LC20();
        lc20.rotate(new int[][]{{1, 2}, {3, 4}});
        lc20.rotate(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
        lc20.rotate(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}});
    }

    public void rotate(int[][] matrix) {
        for (int[] ints : matrix) {
            System.out.println(Arrays.toString(ints));
        }
        System.out.println("--------");
        int n = matrix.length;
        for (int h = 0; h < n / 2; h++) {
            for (int l = 0; l < (n + 1) / 2; l++) {
                int temp = matrix[h][l];
                matrix[h][l] = matrix[n - 1 - l][h];
                matrix[n - 1 - l][h] = matrix[n - 1 - h][n - 1 - l];
                matrix[n - 1 - h][n - 1 - l] = matrix[l][n - 1 - h];
                matrix[l][n - 1 - h] = temp;
                for (int[] ints : matrix) {
                    System.out.println(Arrays.toString(ints));
                }
                System.out.println("--------");
            }
        }
        System.out.println("=======");
    }

    public void rotate1(int[][] matrix) {
        int n = matrix.length;
        for (int h = 0; h < n / 2; ++h) {
            for (int l = 0; l < (n + 1) / 2; ++l) {
                int temp = matrix[h][l];
                matrix[h][l] = matrix[n - l - 1][h];
                matrix[n - l - 1][h] = matrix[n - h - 1][n - l - 1];
                matrix[n - h - 1][n - l - 1] = matrix[l][n - h - 1];
                matrix[l][n - h - 1] = temp;
                for (int[] ints : matrix) {
                    System.out.println(Arrays.toString(ints));
                }
                System.out.println("--------");
            }
        }
    }
}
